package oocl.ita.eordering.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.databind.ObjectMapper;

import oocl.ita.eordering.dao.OrderDao;
import oocl.ita.eordering.po.Order;
import oocl.ita.eordering.po.Page;
import oocl.ita.eordering.service.OrderService;
import oocl.ita.eordering.vo.Vfood;
import oocl.ita.eordering.vo.Vorder;

@Service("os")
public class OrderServiceImpl implements OrderService {
	@Autowired
	private OrderDao od;

	@Override
	@Transactional
	public int acceptOrder(Integer oId) {
		// TODO Auto-generated method stub
		return od.updateOrderStatusToTwo(oId);
	}

	@Override
	@Transactional
	public int refuseOrder(Integer oId) {
		// TODO Auto-generated method stub
		return od.updateOrderStatusToFour(oId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Vorder> findOrderByStatus(Integer mId, Integer oStatus) {
		// TODO Auto-generated method stub
		List<Order> orders = od.findOrderByStatus(mId, oStatus);
		List<Vorder> vorders = new ArrayList<>();
		ObjectMapper mapper = new ObjectMapper();
		for (Order order : orders) {
			Vorder vorder = new Vorder(order);
			try {
				List<Vfood> foodlist = mapper.readValue(order.getFoodInfo(), List.class);
				vorder.setFoodlist(foodlist);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			vorders.add(vorder);
		}
		return vorders;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Vorder> findHistoryOrders(Integer mId) {
		List<Order> orders = od.findHistoryOrders(mId);
		List<Vorder> vorders = new ArrayList<>();
		ObjectMapper mapper = new ObjectMapper();
		for (Order order : orders) {
			Vorder vorder = new Vorder(order);
			List<Vfood> foodlist = new ArrayList<Vfood>();
			try {
				foodlist = mapper.readValue(order.getFoodInfo(), List.class);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			vorder.setFoodlist(foodlist);
			vorders.add(vorder);

		}
		return vorders;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Vorder> findComplaintOrders(Integer mId) {
		// TODO Auto-generated method stub
		List<Order> orders = od.findComplaintOrders(mId);
		List<Vorder> vorders = new ArrayList<>();
		ObjectMapper mapper = new ObjectMapper();
		for (Order order : orders) {
			Vorder vorder = new Vorder(order);
			List<Vfood> foodlist = new ArrayList<Vfood>();
			try {
				foodlist = mapper.readValue(order.getFoodInfo(), List.class);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			vorder.setFoodlist(foodlist);
			vorders.add(vorder);

		}
		return vorders;
	}

	@Override
	public List<Vorder> pageHistoryOrders(Page p, Integer mId) {
		List<Order> orders = od.pageHistoryOrders(p, mId);
		List<Vorder> vorders = new ArrayList<>();
		ObjectMapper mapper = new ObjectMapper();
		for (Order order : orders) {
			Vorder vorder = new Vorder(order);
			List<Vfood> foodlist = new ArrayList<Vfood>();
			try {
				foodlist = (List<Vfood>)mapper.readValue(order.getFoodInfo(), ArrayList.class);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			vorder.setFoodlist(foodlist);
			vorders.add(vorder);

		}
		return vorders;
	}

	@Override
	public int countHistoryOrders(Integer mId) {
		return od.countHistoryOrders(mId);
	}

}
